﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TinyERP.Core.DAO;
using TinyERP.Core.Entity;
using TinyERP.Framework.Config;
using Bamboo.Core.Data;
using System.Collections;

namespace TinyERP.Core
{
    public interface IModuleRegisterService
    {
        void RegisterModuleConfig(ModuleElement moduleConfig);
    }
    public class ModuleRegisterService : IModuleRegisterService
    {
        private IModuleDAO moduleDAO;
        public IModuleDAO ModuleDAO
        {
            get
            {
                return moduleDAO;
            }
            set
            {
                moduleDAO = value;
            }
        }
        public IUINodeDAO UINodeDAO { get; set; }
        public IUserGroupDAO UserGroupDAO { get; set; }
        public IGroupACLDAO GroupACLDAO { get; set; }
        public void RegisterModuleConfig(ModuleElement moduleConfig)
        {
            if (ModuleDAO.GetModuleByAssemblyName(moduleConfig.UIAssembly) == null)
            {
                Module module = new Module();
                module.Assembly = moduleConfig.UIAssembly;
                module.Name = moduleConfig.Name;
                ModuleDAO.SaveModule(module);
                if (moduleConfig.ApplicationNodes != null)
                {
                    foreach (ApplicationNodeElement appNode in moduleConfig.ApplicationNodes)
                    {
                        RegisterAppNode(appNode, module, null);
                    }
                }
            }
        }

        private void RegisterAppNode(ApplicationNodeElement appNode, Module module, UINode parentNode)
        {
            UINode node = UINodeDAO.GetUINodeByName(appNode.ID);
            if (node == null)
            {
                node = new UINode();
                node.Description = appNode.Description;
                node.IconKey = appNode.IconKey;
                node.ResourceKey = appNode.ResourceKey;
                node.Title = appNode.Title;
                node.Name = appNode.ID;
                node.UIClassName = appNode.UIClassName;
                node.Module = module;
                node.Parent = parentNode;
                node.Visible = true;
                long nodeID = (long)UINodeDAO.SaveUINode(node);
                node.ID = nodeID;
                node.Priority = Convert.ToInt32(node.ID);
                UINodeDAO.UpdateUINode(node);
                long totalRecord = 0;
                IList groupList = UserGroupDAO.GetAllUserGroup("ID", SortDirection.ASCENDING, out totalRecord);
                foreach (Group group in groupList)
                {
                    GroupACL groupACL = new GroupACL();
                    groupACL.Group = group;
                    groupACL.UINode = node;
                    GroupACLDAO.SaveGroupACL(groupACL);
                }
                if (appNode.Children != null)
                {
                    foreach (ApplicationNodeElement childNode in appNode.Children)
                    {
                        RegisterAppNode(childNode, module, node);
                    }
                }
            }
        }
    }
}
